---@class TBS.EffectBase
---@field New fun() : TBS.EffectBase
local EffectBase = TBS.CreateClass("EffectBase")
function EffectBase:ctor(id,_type)
    self.id = id or 0
    self.effectType = _type
end
function EffectBase:OnCreate()
    self.resName = ""
    self.loadRequestId = 0
    ---@type Transform
    self.bindTran = nil
    self.isPlaying = false
    self.animName = ""
    ---@type GameObject
    self.obj = nil
    self.isDestroy = false
    self.isActive = true
    self.isLoading = false
    self.pos = Vector3.GetCacheVec()
    self.casterId = 0   --施法者
    self.targetId = 0   --目标对象
    self.skillId = 0    --产生于哪个技能id
    self.during = -1
    self.scale = 1
    self.rotate = 0
    ---@type Spine.Unity.SkeletonAnimation
    self.anim = nil
    self.curTime = 0
    self.isDie = false
    self.sortingOrder = 0
    self.color = nil
    self.isAutoDestroy = true

    self.isDestroying = false
    self.delayDieTime = 0
    self.dieTime = 0
    self.animEventCallback = nil
    self.effectLoadDoneCallback = nil
    self.effectLoadDoneCallbackTB = nil
end
function EffectBase:OnDestroy()
    self.isDestroy = true
    self.isDie = true
    if self.anim then
        self.anim:UnRegisterStateLuaUserFunction()
    end
    TBS.PoolMgr.CancelLoad(self.loadRequestId)
    Vector3.ReturnToPool(self.pos)
    if string.Contains(self.resName,"ColBossSkill9102f") then
        destroy(self.obj)
    else
        TBS.PoolMgr.ReturnPrefabToPool(self.resName,self.obj)
    end

    self.obj = nil
    self.pos = nil
    self.casterId = 0
    self.targetId = 0
    self.skillId = 0
    self.loadRequestId = 0
    self.anim = nil
    self.animEventCallback = nil
end
function EffectBase:IsDie()
    return self.isDie
end
function EffectBase:Die()
    self.isDie = true
    self.isDestroying = false
end
function EffectBase:IsPlaying()
    return self.isPlaying
end
function EffectBase:SetAutoDestroy(isAuto)
    self.isAutoDestroy = isAuto
    if isAuto then
        self.during = self:GetAnimStateDuring(self.animName)
    else
        self.during = -1
    end
end
function EffectBase:SetScale(scale)
    self.scale = scale
    if self.obj then
        Util.Lua_SetLocalScaleXYZ(self.obj,self.scale,self.scale,self.scale)
    end
end
function EffectBase:SetRotate(rotate)
    self.rotate = rotate
    if self.obj then
        Util.Lua_SetLocalRotationXYZ(self.obj,0,0,self.rotate)
    end
end
function EffectBase:GetAnimStateDuring(name)
    if self.anim then
        local anim = self.anim.skeleton.Data:FindAnimation(name)
        if anim then
            return anim.Duration
        end
        logError(self.obj.name.."找不到动作：".." name:"..name)
    end
    return 0.5
end
function EffectBase:SetEffectLoadDoneCallback(callback ,tb)
    self.effectLoadDoneCallback = callback
    self.effectLoadDoneCallbackTB = tb
end
function EffectBase:Reset()
    self.isPlaying = false
    self.isDie = false
    self:SetActive(true)
end
function EffectBase:Play()
    self.isPlaying = true
    if self.resName == "" then
        return
    end
    self.curTime = 0
    self.isDie = false
    if self.isLoading then
        self:SetActive(true)
        self:PlayAnim(self.animName)
        self:SetObjPos(self.pos)
    else
        self.isLoading = true
        self.loadRequestId = TBS.PoolMgr.LoadPrefabFromPool(self.resName,self.LoadCallback,self)
    end
end
function EffectBase:LoadCallback(obj)
    self.obj = obj
    TBS.Battle.AddToScene(self.obj)
    Util.Lua_SetLocalScaleXYZ(self.obj,self.scale,self.scale,self.scale)
    Util.Lua_SetLocalRotationXYZ(self.obj,0,0,self.rotate)
    local anim = obj:GetComponentInChildren(TSkeletonAnimation)
    if anim then
        --Util.Lua_SetLocalScaleXYZ(anim.transform,1,1,1)
        self.anim = anim
    end
    self:OnLoadDone()
    self:SetObjPos(self.pos)
    self:PlayAnim(self.animName)
end
function EffectBase:OnLoadDone()
    self:DoneCallback()
end
function EffectBase:DoneCallback()
    if self.effectLoadDoneCallback then
        self.effectLoadDoneCallback(self.effectLoadDoneCallbackTB)
    end
end
function EffectBase:PlayAnim(animName)
    if  self.anim and not string.IsNilOrEmpty(animName) then
        self.animName = animName
        local time = Util.PlayAction(self.anim,animName,true,-1,true)
        if self.animEventCallback then
            self.anim:RegisterStateLuaUserFunction(self.animEventCallback)
        end
        if self.isAutoDestroy then
            self.during = self:GetAnimStateDuring(animName)
        end
    end
end
---@return TBS.Skill
function EffectBase:GetSkill()
    local caster = self:GetCaster()
    local skill = caster:GetSkill(self.skillId)
    return skill
end
---@return TBS.Player
function EffectBase:GetCaster()
    return TBS.ActorMgr.GetActor(self.casterId)
end
---@return TBS.Player
function EffectBase:GetTarget()
    return TBS.ActorMgr.GetActor(self.targetId)
end
function EffectBase:SetAnimEvent(animationEvent)
    self.animEventCallback = animationEvent
end
function EffectBase:SetObjPos(pos)
    if self.obj then
        Util.Lua_SetLocalPositionXYZ(self.obj,pos.x, pos.y,pos.z)
    end
end

function EffectBase:SetActive(active)
    if self.isActive == active then
        return
    end
    self.isActive = active
    if self.obj then
        self.obj:SetActive(active)
    end
end

function EffectBase:SetDuring(during)
    self.during = during
    self.curTime = 0
end
function EffectBase:Update(dt)
    self.curTime = self.curTime + dt
    if self.during >=0 and self.curTime > self.during then
        self:Die()
        return
    end
end
